#define _CRT_SECURE_NO_WARNINGS   1

#include<iostream>
using namespace std;
int main()
{
	long long int q, a, b, p;
	long long int res;
	cin >> q;
	while (q--)
	{
		cin >> a >> b >> p;
		res = 0;
		while (b)
		{
			res += (a%p);
			b--;
		}
		cout << res%p << endl;
	}
	return 0;
}